<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">
    var line;
        
    function test_MultiLineString_constructor (t) {
        t.plan( 3 );
        mline = new OpenLayers.Geometry.MultiLineString();
        t.ok( mline instanceof OpenLayers.Geometry.MultiLineString, "new OpenLayers.Geometry.MultiLineString returns mline object" );
        t.eq( mline.CLASS_NAME, "OpenLayers.Geometry.MultiLineString", "mline.CLASS_NAME is set correctly");
        t.eq( mline.components, [], "line.components is set correctly");
    }

    function test_MultiLineString_constructor (t) {
        t.plan( 3 );
        line = new OpenLayers.Geometry.LineString();
        mline = new OpenLayers.Geometry.MultiLineString(line);
        t.ok( mline instanceof OpenLayers.Geometry.MultiLineString, "new OpenLayers.Geometry.MultiLineString returns mline object" );
        t.eq( mline.CLASS_NAME, "OpenLayers.Geometry.MultiLineString", "mline.CLASS_NAME is set correctly");
        t.eq( mline.components.length, 1, "mline.components.length is set correctly");
    }

    function test_split(t) {
        var wkt = OpenLayers.Geometry.fromWKT;
        
        var cases = [{
            msg: "no intersection",
            g1: "MULTILINESTRING((0 0, 0 1), (2 2, 3 3))",
            g2: "MULTILINESTRING((1 0, 1 1), (2 2, 3 2))",
            exp: null
        } , {
            msg: "intersection at midpoint",
            g1: "MULTILINESTRING((0 0, 1 1))",
            g2: "MULTILINESTRING((1 0, 0 1))",
            exp: ["MULTILINESTRING((1 0, 0.5 0.5))", "MULTILINESTRING((0.5 0.5, 0 1))"]
        }, {
            msg: "intersection at midpoint (reverse source/target)",
            g1: "MULTILINESTRING((1 0, 0 1))",
            g2: "MULTILINESTRING((0 0, 1 1))",
            exp: ["MULTILINESTRING((0 0, 0.5 0.5))", "MULTILINESTRING((0.5 0.5, 1 1))"]
        }, {
            msg: "intersection at endpoint",
            g1: "MULTILINESTRING((0 0, 1 1))",
            g2: "MULTILINESTRING((1 0, 1 1))",
            exp: null
        }, {
            msg: "midpoint intersection, no options",
            g1: "MULTILINESTRING((0 0, 2 2))",
            g2: "MULTILINESTRING((0 2, 2 0))",
            exp: ["MULTILINESTRING((0 2, 1 1))", "MULTILINESTRING((1 1, 2 0))"]
        }, {
            msg: "midpoint intersection, edge false",
            opt: {edge: false},
            g1: "MULTILINESTRING((0 0, 2 2))",
            g2: "MULTILINESTRING((0 2, 2 0))",
            exp: null
        }, {
            msg: "midpoint intersection, mutual",
            opt: {mutual: true},
            g1: "MULTILINESTRING((0 0, 2 2))",
            g2: "MULTILINESTRING((0 2, 2 0))",
            exp: [["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2))"], ["MULTILINESTRING((0 2, 1 1))", "MULTILINESTRING((1 1, 2 0))"]]
        }, {
            msg: "close intersection, no tolerance",
            g1: "MULTILINESTRING((0 0, 0.9 0.9))",
            g2: "MULTILINESTRING((0 2, 2 0))",
            exp: null
        }, {
            msg: "close intersection, within tolerance",
            opt: {tolerance: 0.2},
            g1: "MULTILINESTRING((0 0, 0.9 0.9))",
            g2: "MULTILINESTRING((0 2, 2 0))",
            exp: ["MULTILINESTRING((0 2, 0.9 0.9))", "MULTILINESTRING((0.9 0.9, 2 0))"]
        }, {
            msg: "multi source, single target",
            g1: "MULTILINESTRING((0 0, 2 2))",
            g2: "LINESTRING(0 2, 2 0)",
            exp: ["LINESTRING(0 2, 1 1)", "LINESTRING(1 1, 2 0)"]
        }, {
            msg: "multi source, single target, mutual split",
            opt: {mutual: true},
            g1: "MULTILINESTRING((0 0, 2 2))",
            g2: "LINESTRING(0 2, 2 0)",
            exp: [["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2))"], ["LINESTRING(0 2, 1 1)", "LINESTRING(1 1, 2 0)"]]
        }, {
            msg: "single source, multi target",
            g1: "LINESTRING(0 2, 2 0)",
            g2: "MULTILINESTRING((0 0, 2 2))",
            exp: ["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2))"]
        }, {
            msg: "partial target split",
            g1: "MULTILINESTRING((2 0, 0 2))",
            g2: "MULTILINESTRING((0 0, 2 2), (3 3, 4 4))",
            exp: ["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2), (3 3, 4 4))"]
        }, {
            msg: "partial target split, mutual true",
            opt: {mutual: true},
            g1: "MULTILINESTRING((2 0, 0 2))",
            g2: "MULTILINESTRING((0 0, 2 2), (3 3, 4 4))",
            exp: [["MULTILINESTRING((2 0, 1 1))", "MULTILINESTRING((1 1, 0 2))"], ["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2), (3 3, 4 4))"]]
        }, {
            msg: "partial source split, mutual true",
            opt: {mutual: true},
            g1: "MULTILINESTRING((0 0, 2 2), (3 3, 4 4))",
            g2: "MULTILINESTRING((2 0, 0 2))",
            exp: [["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2), (3 3, 4 4))"], ["MULTILINESTRING((2 0, 1 1))", "MULTILINESTRING((1 1, 0 2))"]]
        }, {
            msg: "partial target split with source endpoint",
            g1: "MULTILINESTRING((1 0, 1 1))",
            g2: "MULTILINESTRING((0 0, 2 2), (3 3, 4 4))",
            exp: ["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2), (3 3, 4 4))"]
        }, {
            msg: "partial target split with source endpoint, mutual true",
            opt: {mutual: true},
            g1: "MULTILINESTRING((5 5, 6 6), (1 0, 1 1))",
            g2: "MULTILINESTRING((0 0, 2 2), (3 3, 4 4))",
            exp: [[], ["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2), (3 3, 4 4))"]]
        }, {
            msg: "partial source split with target endpoint",
            g1: "MULTILINESTRING((0 0, 2 2), (3 3, 4 4))",
            g2: "MULTILINESTRING((1 0, 1 1))",
            exp: null
        }, {
            msg: "partial source split with target endpoint, mutual true",
            opt: {mutual: true},
            g1: "MULTILINESTRING((0 0, 2 2), (3 3, 4 4), (5 5, 6 6))",
            g2: "MULTILINESTRING((1 0, 1 1))",
            exp: [["MULTILINESTRING((0 0, 1 1))", "MULTILINESTRING((1 1, 2 2), (3 3, 4 4), (5 5, 6 6))"], []]
        }, {
            msg: "partial target and source split",
            g1: "MULTILINESTRING((0 5, 2 5), (4 5, 6 5), (8 5, 10 5))",
            g2: "MULTILINESTRING((5 0, 5 2), (5 4, 5 6), (5 8, 5 10))",
            exp: ["MULTILINESTRING((5 0, 5 2), (5 4, 5 5))", "MULTILINESTRING((5 5, 5 6), (5 8, 5 10))"]
        }, {
            msg: "partial target and source split, mutual true",
            opt: {mutual: true},
            g1: "MULTILINESTRING((0 5, 2 5), (4 5, 6 5), (8 5, 10 5))",
            g2: "MULTILINESTRING((5 0, 5 2), (5 4, 5 6), (5 8, 5 10))",
            exp: [["MULTILINESTRING((0 5, 2 5), (4 5, 5 5))", "MULTILINESTRING((5 5, 6 5), (8 5, 10 5))"],
                  ["MULTILINESTRING((5 0, 5 2), (5 4, 5 5))", "MULTILINESTRING((5 5, 5 6), (5 8, 5 10))"]]
        }, {
            msg: "partial target and source split with source endpoint, mutual true",
            opt: {mutual: true},
            g1: "MULTILINESTRING((0 5, 2 5), (4 5, 6 5), (8 5, 10 5))",
            g2: "MULTILINESTRING((4 0, 4 2), (4 4, 4 6), (4 8, 4 10))",
            exp: [[], ["MULTILINESTRING((4 0, 4 2), (4 4, 4 5))", "MULTILINESTRING((4 5, 4 6), (4 8, 4 10))"]]
        }, {
            msg: "partial target and source split with target endpoint, mutual true",
            opt: {mutual: true},
            g1: "MULTILINESTRING((4 0, 4 2), (4 4, 4 6), (4 8, 4 10))",
            g2: "MULTILINESTRING((0 5, 2 5), (4 5, 6 5), (8 5, 10 5))",
            exp: [["MULTILINESTRING((4 0, 4 2), (4 4, 4 5))", "MULTILINESTRING((4 5, 4 6), (4 8, 4 10))"], []]
        }, {
            msg: "partial target and source split with source vertex, mutual true",
            opt: {mutual: true},
            g1: "MULTILINESTRING((0 5, 2 5), (4 5, 5 5, 6 5), (8 5, 10 5))",
            g2: "MULTILINESTRING((5 0, 5 2), (5 4, 5 6), (5 8, 5 10))",
            exp: [["MULTILINESTRING((0 5, 2 5), (4 5, 5 5))", "MULTILINESTRING((5 5, 6 5), (8 5, 10 5))"], ["MULTILINESTRING((5 0, 5 2), (5 4, 5 5))", "MULTILINESTRING((5 5, 5 6), (5 8, 5 10))"]]
        }, {
            msg: "partial target and source split with target vertex, mutual true",
            opt: {mutual: true},
            g1: "MULTILINESTRING((5 0, 5 2), (5 4, 5 6), (5 8, 5 10))",
            g2: "MULTILINESTRING((0 5, 2 5), (4 5, 5 5, 6 5), (8 5, 10 5))",
            exp: [["MULTILINESTRING((5 0, 5 2), (5 4, 5 5))", "MULTILINESTRING((5 5, 5 6), (5 8, 5 10))"], ["MULTILINESTRING((0 5, 2 5), (4 5, 5 5))", "MULTILINESTRING((5 5, 6 5), (8 5, 10 5))"]]
        }];


        t.plan(cases.length);
        var c, parts, part, midparts;
        for(var i=0; i<cases.length; ++i) {
            c = cases[i];
            var g1 = wkt(c.g1);
            var g2 = wkt(c.g2);
            var got = g1.split(g2, c.opt);
            var exp = c.exp;
            if(got instanceof Array) {
                parts = [];
                for(var j=0; j<got.length; ++j) {
                    part = got[j];
                    if(part instanceof Array) {
                        midparts = [];
                        for(var k=0; k<part.length; ++k) {
                            midparts.push(part[k].toString());
                        }
                        parts.push("[" + midparts.join(", ") + "]");
                    } else {
                        parts.push(got[j].toString());
                    }
                }
                got = parts.join(", ");
            }
            if(exp instanceof Array) {
                parts = [];
                for(var j=0; j<exp.length; ++j) {
                    part = exp[j];
                    if(part instanceof Array) {
                        midparts = [];
                        for(var k=0; k<part.length; ++k) {
                            midparts.push(wkt(part[k]).toString());
                        }
                        parts.push("[" + midparts.join(", ") + "]");
                    } else {
                        parts.push(wkt(exp[j]).toString());
                    }
                }
                exp = parts.join(", ");
            }
            t.eq(got, exp, "case " + i + ": " +  c.msg);
        }
        
    }

    function test_getVertices(t) {
        t.plan(22);
        
        var points = [
            new OpenLayers.Geometry.Point(10, 20),
            new OpenLayers.Geometry.Point(20, 30),
            new OpenLayers.Geometry.Point(30, 40),
            new OpenLayers.Geometry.Point(40, 50)
        ];
        
        var multi = new OpenLayers.Geometry.MultiLineString([
            new OpenLayers.Geometry.LineString(points),
            new OpenLayers.Geometry.LineString(points)
        ]);
        
        var verts = multi.getVertices();
        t.ok(verts instanceof Array, "got back an array");
        t.eq(verts.length, 2 * points.length, "of correct length length");
        t.geom_eq(verts[0], points[0], "0: correct geometry");
        t.geom_eq(verts[1], points[1], "1: correct geometry");
        t.geom_eq(verts[2], points[2], "2: correct geometry");
        t.geom_eq(verts[3], points[3], "3: correct geometry");
        t.geom_eq(verts[4], points[0], "4: correct geometry");
        t.geom_eq(verts[5], points[1], "5: correct geometry");
        t.geom_eq(verts[6], points[2], "6: correct geometry");
        t.geom_eq(verts[7], points[3], "7: correct geometry");
        
        // nodes only
        var nodes = multi.getVertices(true);
        t.ok(nodes instanceof Array, "[nodes only] got back an array");
        t.eq(nodes.length, 4, "[nodes only] of correct length length");
        t.geom_eq(nodes[0], points[0], "[nodes only] 0: correct geometry");
        t.geom_eq(nodes[1], points[3], "[nodes only] 1: correct geometry");
        t.geom_eq(nodes[2], points[0], "[nodes only] 2: correct geometry");
        t.geom_eq(nodes[3], points[3], "[nodes only] 3: correct geometry");
        
        // no nodes
        var nodes = multi.getVertices(false);
        t.ok(nodes instanceof Array, "[no nodes] got back an array");
        t.eq(nodes.length, 4, "[no nodes] of correct length length");
        t.geom_eq(nodes[0], points[1], "[no nodes] 0: correct geometry");
        t.geom_eq(nodes[1], points[2], "[no nodes] 1: correct geometry");
        t.geom_eq(nodes[2], points[1], "[no nodes] 2: correct geometry");
        t.geom_eq(nodes[3], points[2], "[no nodes] 3: correct geometry");


    }


  </script>
</head>
<body>
</body>
</html>
